package com.bloveoacom.base;


import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.bloveoacom.mvp.BaseListPresenter;
import com.bloveoacom.mvp.IListView;
import com.bloveoacom.mvp.IModel;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weight.CustomLoadMoreView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.bloveoacom.http.HttpMethods.PAGE_START_SIZE;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/5/9
 * desc  :
 */
public abstract class BaseList2Fragment<T> extends BaseFragment implements IListView,
        BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.recycle_content)
    public RecyclerView mRecyclerView;
    @BindView(R.id.swipe_content)
    public SwipeRefreshLayout mSwipeRefresh;


    /**
     * 适配器
     */
    protected BaseQuickAdapter<T, BaseViewHolder> adapter;
    protected List<T> datas = new ArrayList<>();
    protected int page = PAGE_START_SIZE;



    private int pageSize = 10;


    protected abstract int getItemLayout();

    protected abstract void convertView(BaseViewHolder helper, T item);

    protected void setLayoutManager() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
    }
    @Override
    public void initView() {
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(null);
        adapter = new BaseQuickAdapter<T, BaseViewHolder>(getItemLayout(), datas) {
            @Override
            protected void convert(BaseViewHolder helper, T item) {
                convertView(helper, item);
            }
        };

        if (isOpenRefresh()) {
            if (mSwipeRefresh != null) {
                mSwipeRefresh.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.colorPrimary));
                mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        refresh();
                    }
                });
            }
        } else {
            if (mSwipeRefresh != null) {
                mSwipeRefresh.setEnabled(false);
            }
        }

        if (isLoadMore()) {
            adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    loadMore();
                }
            }, mRecyclerView);
        }
        addViewForAdapter();
        adapter.setLoadMoreView(new CustomLoadMoreView());
        setLayoutManager();
        mRecyclerView.setAdapter(adapter);
    }

    public boolean isLoadMore() {
        return true;
    }

    public void addViewForAdapter(){

    }

    @Override
    public void initData() {
        refresh();
    }
    protected boolean isOpenRefresh() {
        return true;
    }

    @Override
    public void finishRefresh() {
        if (mSwipeRefresh != null && isOpenRefresh()) {
            mSwipeRefresh.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mSwipeRefresh != null && isOpenRefresh()) {
                        mSwipeRefresh.setRefreshing(false);
                        mSwipeRefresh.setEnabled(true);
                    }
                }
            }, 500);
        }
        adapter.setEnableLoadMore(true);
    }

    @Override
    public void showRefreshLoading() {
        adapter.setEnableLoadMore(false);
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }


    public int getPageSize() {
        return pageSize;
    }
    private void loadMore() {
        if (mSwipeRefresh.isEnabled()) {
            mSwipeRefresh.setEnabled(false);
        }
        page++;
        getData();
    }

    public void refresh() {
        if (mContext == null)
            return;
        if (adapter == null) {
            return;
        }
        if (adapter.isLoadMoreEnable()) {
            adapter.setEnableLoadMore(false);
        }
        if (isOpenRefresh()) {
            mSwipeRefresh.setRefreshing(true);
        }
        page = 1;
        getData();
    }
    protected abstract void getData();

    @Override
    public void loadMoreFail() {
        adapter.loadMoreFail();
    }

    public void doData(List<T> data) {
        if (page <= 1) {
            datas.clear();
            // 设置加载更多能用
            if (mSwipeRefresh.isRefreshing()) {
                mSwipeRefresh.setRefreshing(false);
            }
            datas.addAll(data);
            adapter.notifyDataSetChanged();
            adapter.setEnableLoadMore(true);
        } else {
            if (data == null || data.size() == 0) {
                adapter.loadMoreEnd();
            } else {
                datas.addAll(data);
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }
            if (!mSwipeRefresh.isEnabled())
                mSwipeRefresh.setEnabled(true);

        }

    }
}
